﻿namespace JelloPhysics
{
    using Microsoft.Xna.Framework;
    using System;
    using System.Collections.Generic;

    public class ClosedShape
    {
        private List<Vector2> mLocalVertices;

        public ClosedShape()
        {
            this.mLocalVertices = new List<Vector2>();
        }

        public ClosedShape(List<Vector2> verts)
        {
            this.mLocalVertices = new List<Vector2>(verts);
            this.finish();
        }

        public int addVertex(Vector2 vert)
        {
            this.mLocalVertices.Add(vert);
            return this.mLocalVertices.Count;
        }

        public void begin()
        {
            this.mLocalVertices.Clear();
        }

        public void finish()
        {
            this.finish(true);
        }

        public void finish(bool recenter)
        {
            if (recenter)
            {
                Vector2 zero = Vector2.Zero;
                for (int i = 0; i < this.mLocalVertices.Count; i++)
                {
                    zero += this.mLocalVertices[i];
                }
                zero = (Vector2) (zero / ((float) this.mLocalVertices.Count));
                for (int j = 0; j < this.mLocalVertices.Count; j++)
                {
                    List<Vector2> list;
                    int num3;
                    (list = this.mLocalVertices)[num3 = j] = list[num3] - zero;
                }
            }
        }

        public List<Vector2> transformVertices(Vector2 worldPos, float angleInRadians, Vector2 localScale)
        {
            List<Vector2> list = new List<Vector2>(this.mLocalVertices);
            Vector2 vecIn = new Vector2();
            for (int i = 0; i < list.Count; i++)
            {
                vecIn.X = list[i].X * localScale.X;
                vecIn.Y = list[i].Y * localScale.Y;
                VectorTools.rotateVector(ref vecIn, angleInRadians, ref vecIn);
                vecIn.X += worldPos.X;
                vecIn.Y += worldPos.Y;
                list[i] = vecIn;
            }
            return list;
        }

        public void transformVertices(ref Vector2 worldPos, float angleInRadians, ref Vector2 localScale, ref Vector2[] outList)
        {
            for (int i = 0; i < this.mLocalVertices.Count; i++)
            {
                Vector2 vecInOut = new Vector2();
                vecInOut.X = this.mLocalVertices[i].X * localScale.X;
                vecInOut.Y = this.mLocalVertices[i].Y * localScale.Y;
                VectorTools.rotateVector(ref vecInOut, angleInRadians);
                vecInOut.X += worldPos.X;
                vecInOut.Y += worldPos.Y;
                outList[i].X = vecInOut.X;
                outList[i].Y = vecInOut.Y;
            }
        }

        public List<Vector2> Vertices
        {
            get
            {
                return this.mLocalVertices;
            }
        }
    }
}

